import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/utils/tools_util.dart';

import 'safe_appbar.dart';

/// 安全脚手架
class SafeScaffold extends StatelessWidget {
  const SafeScaffold({
    Key? key,
    this.appBar,
    this.body,
    this.floatingActionButton,
    this.floatingActionButtonLocation,
    this.floatingActionButtonAnimator,
    this.persistentFooterButtons,
    this.drawer,
    this.onDrawerChanged,
    this.endDrawer,
    this.onEndDrawerChanged,
    this.bottomNavigationBar,
    this.bottomSheet,
    this.backgroundColor,
    this.resizeToAvoidBottomInset,
    this.primary = true,
    this.drawerDragStartBehavior = DragStartBehavior.start,
    this.extendBody = false,
    this.extendBodyBehindAppBar = false,
    this.drawerScrimColor,
    this.drawerEdgeDragWidth,
    this.drawerEnableOpenDragGesture = true,
    this.endDrawerEnableOpenDragGesture = true,
    this.restorationId,
    this.tapToCloseKeyboard = false,
    this.autoAppBar = true,
    this.autoBackIcon = true,
    this.titleText,
    this.actions,
    this.overlayStyle = SystemUiOverlayStyle.dark,
    this.appbarColor,
    this.systemNavigationBarColor,
    this.systemNavigationBarBrightness,
  }) : super(key: key);

  final bool extendBody;
  final bool extendBodyBehindAppBar;
  final PreferredSizeWidget? appBar;
  final Widget? body;
  final Widget? floatingActionButton;
  final FloatingActionButtonLocation? floatingActionButtonLocation;
  final FloatingActionButtonAnimator? floatingActionButtonAnimator;
  final List<Widget>? persistentFooterButtons;
  final Widget? drawer;
  final DrawerCallback? onDrawerChanged;
  final Widget? endDrawer;
  final DrawerCallback? onEndDrawerChanged;
  final Color? drawerScrimColor;
  final Color? backgroundColor;
  final Widget? bottomNavigationBar;
  final Widget? bottomSheet;
  final bool? resizeToAvoidBottomInset;
  final bool primary;
  final DragStartBehavior drawerDragStartBehavior;
  final double? drawerEdgeDragWidth;
  final bool drawerEnableOpenDragGesture;
  final bool endDrawerEnableOpenDragGesture;
  final String? restorationId;

  /// 点击以关闭键盘
  final bool tapToCloseKeyboard;

  /// 自动添加appbar
  final bool autoAppBar;

  /// 自动添加返回按钮
  final bool autoBackIcon;

  /// 标题文本
  final String? titleText;

  final List<Widget>? actions;

  final SystemUiOverlayStyle overlayStyle;

  final Color? appbarColor;

  final Color? systemNavigationBarColor;

  final Brightness? systemNavigationBarBrightness;

  @override
  Widget build(BuildContext context) {
    Widget scaffold = Scaffold(
      extendBody: extendBody,
      extendBodyBehindAppBar: extendBodyBehindAppBar,
      appBar: _appBar,
      body: body,
      floatingActionButton: floatingActionButton,
      floatingActionButtonLocation: floatingActionButtonLocation,
      floatingActionButtonAnimator: floatingActionButtonAnimator,
      persistentFooterButtons: persistentFooterButtons,
      drawer: drawer,
      onDrawerChanged: onDrawerChanged,
      endDrawer: endDrawer,
      onEndDrawerChanged: onEndDrawerChanged,
      drawerScrimColor: drawerScrimColor,
      backgroundColor: backgroundColor,
      bottomNavigationBar: bottomNavigationBar,
      bottomSheet: bottomSheet,
      resizeToAvoidBottomInset: resizeToAvoidBottomInset,
      primary: primary,
      drawerDragStartBehavior: drawerDragStartBehavior,
      drawerEdgeDragWidth: drawerEdgeDragWidth,
      drawerEnableOpenDragGesture: drawerEnableOpenDragGesture,
      endDrawerEnableOpenDragGesture: endDrawerEnableOpenDragGesture,
      restorationId: restorationId,
    );

    if (tapToCloseKeyboard) {
      scaffold = GestureDetector(
        onTap: () => unFocus(context),
        child: scaffold,
      );
    }

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: overlayStyle.copyWith(
        systemNavigationBarDividerColor: Colors.transparent,
        systemNavigationBarColor: systemNavigationBarColor ?? white(context),
        systemNavigationBarIconBrightness: systemNavigationBarBrightness ?? Brightness.dark,
      ),
      child: scaffold,
    );
  }

  PreferredSizeWidget? get _appBar {
    if (appBar == null && autoAppBar) {
      return SafeAppBar(
        titleText: titleText,
        actions: actions,
        autoBackIcon: autoBackIcon,
        appBarColor: appbarColor,
      );
    }

    return appBar;
  }
}
